package com.itheima.controller;

import com.itheima.utils.AliOSSProperties;
import com.itheima.entity.Result;
import com.itheima.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author 马祥和
 * @version 1.0
 * @function
 * @time 2025/7/25 19:00
 */
@Slf4j
@RestController
public class UploadController {

//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile file) throws IOException {
//        log.info("参数：{}, {}, {} ",username, age, file);
//
//        String originalFilename = file.getOriginalFilename();
//
////        UUID生成随机字符串
//        if (originalFilename != null){
//            String newFileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//            file.transferTo(new File("D:/" + newFileName));
//            return Result.success(newFileName);
//        }
//
//        return Result.error("null");
//    }

//    @Value("${aliyun.oss.bucketName}")
//    private String bucketName;
//
//    @Value("${aliyun.oss.endpoint}")
//    private String endpoint;
    @Autowired
    private AliOSSProperties aliOSSProperties;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("参数：{}", file);

        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));

        String url = AliyunOSSUtils.upload(aliOSSProperties.getEndpoint(), aliOSSProperties.getBucketName(), file.getBytes(), extName);

        return Result.success(url);

    }
}
